package com.agile.leetcode.concurrent.daemon;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author CZK
 * @date 2022/8/9 15:23
 */
public class DeamonThreadPool {
    public static void main(String[] args) throws InterruptedException {
        // 需要使用线程工厂 ThreadFactory 来设置了（线程池中的所有线程都是通过线程工厂创建的）
        ThreadFactory factory = new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                Thread thread = new Thread(r);
                thread.setDaemon(true);
                return thread;
            }
        };
        ThreadPoolExecutor pool = new ThreadPoolExecutor(8,
                8,
                8L,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(),
                factory);
        pool.submit(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().isDaemon() ? "守护线程" : "非守护线程");
            }
        });
        TimeUnit.SECONDS.sleep(1);
    }
}
